/*
 * handle_connect.c
 *
 *  Created on: 2009-5-5
 *      Author: tian
 */

#include"tcpipsim.h"

/* if connect is succeed, tcp_input should send
 * success message to process msg queue directly.
 * this function only calls tcp_input and return
 * failure message.
 */

void handle_connect(msg *pmsg){
	sockmsg retmsg;
	memset(&retmsg,0,sizeof(sockmsg));
	struct tcpcb *ptcpcb=get_tcpcb_by_sock(pmsg->pid,pmsg->sock);
	if(ptcpcb==NULL){
		retmsg.mtype=pmsg->sock;
		retmsg.ret=-1;
		if(msgsnd(pmsg->msgqid,&retmsg,sizeof(sockmsg)-sizeof(long),0,0)<0){
					fprintf(stderr,"can not send message to application layer\n");
					return;
		}
	}
	else{
		if(tcp_input(NULL,NULL,ptcpcb,active_open)<0){
			retmsg.mtype=pmsg->sock;
			retmsg.ret=-1;
			if(msgsnd(pmsg->msgqid,&retmsg,sizeof(sockmsg)-sizeof(long),0,0)<0){
				fprintf(stderr,"can not send message to application layer\n");
				return;
			}
		}
	}
}
